/**
 * CRM模块工具函数
 */

import { ref } from 'vue'

// 客户状态选项
export const customerStatusOptions = [
  { value: 1, label: '正常' },
  { value: 2, label: '已合作' },
  { value: 3, label: '公海客户' },
  { value: 4, label: '已流失' },
]

// 客户类型选项
export const customerTypeOptions = [
  { value: 1, label: '企业客户' },
  { value: 2, label: '个人客户' },
  { value: 3, label: '政府客户' },
]

// 跟进方式选项
export const followupTypeOptions = [
  { value: 1, label: '电话联系' },
  { value: 2, label: '微信沟通' },
  { value: 3, label: '邮件联系' },
  { value: 4, label: '上门拜访' },
  { value: 5, label: '会议沟通' },
]

// 业务类型选项
export const orderTypeOptions = [
  { value: 1, label: '诉讼业务' },
  { value: 2, label: '非诉业务' },
  { value: 3, label: '咨询服务' },
  { value: 4, label: '其他业务' },
]

// 订单状态选项
export const orderStatusOptions = [
  { value: 1, label: '待审核' },
  { value: 2, label: '审核通过' },
  { value: 3, label: '审核驳回' },
  { value: 4, label: '已取消' },
]

// 付款方式选项
export const paymentTypeOptions = [
  { value: 1, label: '银行转账' },
  { value: 2, label: '现金支付' },
  { value: 3, label: '微信支付' },
  { value: 4, label: '支付宝' },
  { value: 5, label: '其他' },
]

// 审核类型选项
export const reviewTypeOptions = [
  { value: 1, label: '业务订单审核' },
  { value: 2, label: '联系人修改审核' },
  { value: 3, label: '联系人删除审核' },
]

// 审核状态选项
export const reviewStatusOptions = [
  { value: 1, label: '待审核' },
  { value: 2, label: '审核通过' },
  { value: 3, label: '审核驳回' },
]

// 获取状态标签
export const getStatusLabel = (value: number, options: any[]) => {
  const option = options.find(item => item.value === value)
  return option ? option.label : '未知'
}

// 获取状态标签类型
export const getStatusTagType = (value: number) => {
  const types = ['', 'warning', 'success', 'danger']
  return types[value] || 'info'
}

// 格式化金额
export const formatAmount = (amount: number) => {
  return new Intl.NumberFormat('zh-CN', {
    style: 'currency',
    currency: 'CNY',
  }).format(amount)
}

// 格式化日期
export const formatDate = (date: string | Date) => {
  if (!date) return '-'
  return new Date(date).toLocaleString('zh-CN')
}

// 省市区数据
export const provinceOptions = ref([
  { value: '110000', label: '北京市' },
  { value: '120000', label: '天津市' },
  { value: '130000', label: '河北省' },
  { value: '140000', label: '山西省' },
  { value: '150000', label: '内蒙古自治区' },
  { value: '210000', label: '辽宁省' },
  { value: '220000', label: '吉林省' },
  { value: '230000', label: '黑龙江省' },
  { value: '310000', label: '上海市' },
  { value: '320000', label: '江苏省' },
  { value: '330000', label: '浙江省' },
  { value: '340000', label: '安徽省' },
  { value: '350000', label: '福建省' },
  { value: '360000', label: '江西省' },
  { value: '370000', label: '山东省' },
  { value: '410000', label: '河南省' },
  { value: '420000', label: '湖北省' },
  { value: '430000', label: '湖南省' },
  { value: '440000', label: '广东省' },
  { value: '450000', label: '广西壮族自治区' },
  { value: '460000', label: '海南省' },
  { value: '500000', label: '重庆市' },
  { value: '510000', label: '四川省' },
  { value: '520000', label: '贵州省' },
  { value: '530000', label: '云南省' },
  { value: '540000', label: '西藏自治区' },
  { value: '610000', label: '陕西省' },
  { value: '620000', label: '甘肃省' },
  { value: '630000', label: '青海省' },
  { value: '640000', label: '宁夏回族自治区' },
  { value: '650000', label: '新疆维吾尔自治区' },
  { value: '710000', label: '台湾省' },
  { value: '810000', label: '香港特别行政区' },
  { value: '820000', label: '澳门特别行政区' },
])

// 权限检查
export const checkPermission = (permission: string) => {
  // 这里需要根据实际权限系统实现
  const userPermissions = [
    'crm:customer:view',
    'crm:customer:edit',
    'crm:order:view',
    'crm:order:edit',
  ]
  return userPermissions.includes(permission)
}

// 数据验证规则
export const validationRules = {
  required: { required: true, message: '此项为必填项', trigger: 'blur' },
  phone: { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
  email: { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' },
  amount: { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的金额格式', trigger: 'blur' },
}
